package com.songlanyun.modules.marketing.seckill.entity;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.SpringContextUtils;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.goods.model.values.SkuSet;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyActivityGoods;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyActivityGoodsService;
import com.songlanyun.modules.marketing.seckill.service.SeckillActivityGoodsService;
import com.songlanyun.modules.singlegoods.entity.ProductSingleGoodsEntity;
import com.songlanyun.modules.singlegoods.service.ProductSingleGoodsService;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.math.BigDecimal;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 秒杀商品的规格表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-23 17:30:11
 */
@Data
@TableName("yxt_marketing_seckill_goods_sku")
public class SeckillGoodsSku extends RootEntity<Long, SeckillGoodsSku> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     *
     */
    @TableId
    private Long id;
    /**
     * 商品id
     */
    private Long goodsId;
    /**
     * 秒杀商品id
     */
    private Long seckillGoodsId;
    /**
     *
     */
    @TableField(exist = false)
    private List<SkuSet> skuSets;

    public void setSkuSets(List<SkuSet> skuSets) {
        this.skuSets = skuSets;

        if (this.skuSets == null || this.skuSets.size() == 0) {
            this.setSkuHashCode("");
            this.skuSetName = "";
            this.skuSetValue = "";
            return;
        }
        List<String> codeList = this.skuSets.stream().map(item -> {
            return item.getValue().getId().toString();
        }).collect(Collectors.toList());

        this.setSkuHashCode(String.join("|", codeList));
        this.skuSetName = StringUtils.join(this.skuSets.stream().map(o -> o.getValue().getName()).toArray(), ";");
        this.skuSetValue = JSONUtil.toJsonStr(this.skuSets);
    }

    @JsonIgnore
    private String skuSetValue;

    public void setSkuSetValue(String skuSetValue) {
        this.skuSetValue = skuSetValue;
        if (StringUtils.isEmpty(this.skuSetValue)) {
            this.skuSets = new ArrayList<>();
        } else {
            try {
                this.skuSets = ClassConvertUtils.stringToClassSnake(skuSetValue, new TypeReference<List<SkuSet>>() {
                });
            } catch (IOException e) {
                throw new RRException("商品规格无法转Map");
            }
        }
    }

    /**
     * sku的字面量（黄色;175）
     */
    private String skuSetName;
    /**
     * 活动价格（要自己算）
     */
    private BigDecimal activityPrice;
    /**
     * sku的hash值(1|2)
     */
    private String skuHashCode;
    /**
     * 原销售价（要用查的）
     */
    private BigDecimal salePrice;

    //==========考贝商品数据，为了不受商品sku组合的变化而影响
    /**
     * 单品id
     */
    private Long singleGoodsId;

    /**
     * 单品码
     */
    private String singleGoodsCode;

    /**
     * 单品名称（容余）
     */
    private String singleGoodsTitle;

    /**
     * 市场价
     */
    private BigDecimal marketPrice;
    /**
     * 重量
     */
    private BigDecimal weight;
    /**
     * 体积
     */
    private BigDecimal volume;


    /**
     * 商品规格编码
     */
    private String barCode;

    /**
     * 封面图片
     */
    private String cover;
    /**
     *
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 库存
     */
    @TableField(exist = false)
    private Integer ableStock;


    @Override
    public Long getIdentity() {
        return id;
    }

    @JsonIgnore
    @TableField(exist = false)
    private ProductSingleGoodsEntity singleGoods;

    public ProductSingleGoodsEntity loadSingleGoods() {

        if (this.singleGoods == null) {
            singleGoods = ((ProductSingleGoodsService) SpringContextUtils.getBean("productSingleGoodsService")).getById(this.getSingleGoodsId());
        }
        return this.singleGoods;
    }

    /**
     * 获得单品成本价
     *
     * @return
     */
    @TableField(exist = false)
    private BigDecimal costPrice;

    public BigDecimal getCostPrice() {
        ProductSingleGoodsEntity singleGoods = loadSingleGoods();
        if (singleGoods == null) {
            return BigDecimal.ZERO;
        }
        return singleGoods.getCostPrice();
    }

    @JsonIgnore
    @TableField(exist = false)
    private SeckillActivityGoods activityGoods;

    public SeckillActivityGoods loadActivityGoods() {

        if (this.activityGoods == null) {
            activityGoods = ((SeckillActivityGoodsService) SpringContextUtils.getBean("seckillActivityGoodsService")).getById(this.getSeckillGoodsId());
        }
        return activityGoods;
    }


    /**
     * 第三方的skuId,用来与第三方下单，发货，售后时用的
     */
    private Long thirdSkuId;

    /**
     * 第三方的成本价
     */
    private BigDecimal  thirdCostPrice;

    /**
     * 规格协议价
     */
    private BigDecimal agreementPrice;

}
